package com.ssm.collection.admin.base;

import java.util.List;

import org.slf4j.Logger;

import com.ssm.collection.core.common.service.BaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.collection.admin.util.SysStr;

public abstract class BaseActionContorller<E> extends AbstractWebController {

	public abstract Logger getLog();

	public abstract BaseService<E, Long> getBaseService();

	public abstract String getTableName();

	public final Integer DELETE_TRUE = 1;

	public Object get(Long id) {
		JsonResult json = new JsonResult();
		json.setMessage(SysStr.SELECT_FAIL);
		boolean result = true;
		E entity = null;
		try {
			getLog().debug("查询的{}id:{}", getTableName(), id);
			entity = getBaseService().getPk(id);
			json.setMessage(SysStr.SELECT_SUCCESS);
		} catch (Exception e) {
			result = false;
			getLog().error("查询{}信息失败！id:{}", getTableName(), id);
			e.printStackTrace();
		}
		json.setData(entity);
		json.setSuccess(result);
		return json;
	}

	public Object getAll(E entity, Integer offset, Integer limit) {
		JsonResult json = new JsonResult();
		json.setMessage(SysStr.SELECT_FAIL);
		boolean result = true;
		List<E> entityList = null;
		PageInfo<E> pageInfo = null;
		try {
			getLog().debug("查询全部:{}", getTableName());
			offset = getParamOffset(offset);
			limit = getParamLimit(limit);
			PageHelper.offsetPage(offset, limit);
			entityList = getBaseService().queryForList(entity);
			pageInfo = new PageInfo<>(entityList);
			getLog().debug("总数量：{}", pageInfo.getTotal());
			json.setMessage(SysStr.SELECT_SUCCESS);
			json.setData(new PaginationInfo(offset, limit, (int) pageInfo.getTotal(), entityList));
		} catch (Exception e) {
			result = false;
			getLog().error("查询全部:{},失败！", getTableName());
			e.printStackTrace();
		}
		json.setSuccess(result);
		return json;
	}

	/**
	 * @param id
	 * @param operator_id
	 * @param is_delete  逻辑删除：小于0  null  物理删除：大于0   非null
	 * @return
	 */
	public Object delete(Long id, Long operator_id, Integer is_delete) {
		String delInfo = "逻辑";
		if (is_delete != null && is_delete > 0) {
			delInfo = "物理";
		}
		JsonResult json = new JsonResult();
		json.setMessage(delInfo + SysStr.DELETE_FAIL);
		boolean result = true;
		try {
			getLog().debug(String.format("删除{}！用户id:%s", getTableName(), id));
			if (is_delete != null && is_delete > 0) {
				getLog().debug("物理删除...is_delete：{}", is_delete);
				getBaseService().delete(id);
			} else {
				getLog().debug("逻辑删除...is_delete：{}", is_delete);
				getBaseService().deleteFlag(id, operator_id);
			}
			json.setMessage(delInfo + SysStr.DELETE_SUCCESS);
		} catch (Exception e) {
			result = false;
			getLog().error("删除{}失败！用户id:{}", getTableName(), id);
			e.printStackTrace();
		}
		json.setSuccess(result);
		return json;
	}

	public Object addOrUpdate(E entrust, Long id) {
		JsonResult json = new JsonResult();
		boolean result = true;
		if (id == null) {
			json.setMessage(SysStr.ADD_FAIL);
			try {
				getLog().debug("新增{}", getTableName());
				getBaseService().insert(entrust);
				json.setMessage(SysStr.ADD_SUCCESS);
			} catch (Exception e) {
				result = false;
				getLog().error("新增{}失败", getTableName());
				e.printStackTrace();
			}
		} else {
			json.setMessage(SysStr.UPDATE_FAIL);
			try {
				getLog().debug("修改{}信息！id:{}", getTableName(), id);
				getBaseService().update(entrust);
				json.setMessage(SysStr.UPDATE_SUCCESS);
			} catch (Exception e) {
				result = false;
				getLog().error("查询{}信息失败！id:{}", getTableName(), id);
				e.printStackTrace();
			}
		}
		json.setSuccess(result);
		return json;
	}
}
